{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据已成功写入汇总文件。\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\code\\AppData\\Local\\Temp\\ipykernel_9280\\551104005.py:33: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n",
      "  summary = pd.concat([summary, pd.DataFrame([new_row])], ignore_index=True)\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "\n",
    "# 指定数据文件夹路径\n",
    "data_folder = 'data'\n",
    "\n",
    "# 获取文件夹中所有.xlsx文件的列表\n",
    "xlsx_files = [f for f in os.listdir(data_folder) if f.endswith('.xlsx')]\n",
    "\n",
    "# 创建一个空的DataFrame来存储汇总数据\n",
    "summary = pd.DataFrame(columns=['日期', '最小', '最大', '平均'])\n",
    "\n",
    "# 遍历每个.xlsx文件\n",
    "for file in xlsx_files:\n",
    "    # 构建完整的文件路径\n",
    "    file_path = os.path.join(data_folder, file)\n",
    "    \n",
    "    # 读取Excel文件\n",
    "    data = pd.read_excel(file_path)\n",
    "    \n",
    "    # 提取温度列中的最小、最大和平均数字\n",
    "    min_temp = data['温度'].min()\n",
    "    max_temp = data['温度'].max()\n",
    "    avg_temp = data['温度'].mean()\n",
    "    \n",
    "    # 获取文件名中的日期部分\n",
    "    date = file.split('.')[0]\n",
    "    \n",
    "    # 创建新的行数据\n",
    "    new_row = {'日期': date, '最小': min_temp, '最大': max_temp, '平均': avg_temp}\n",
    "    \n",
    "    # 添加新的行到汇总DataFrame\n",
    "    summary = pd.concat([summary, pd.DataFrame([new_row])], ignore_index=True)\n",
    "\n",
    "\n",
    "# 写入汇总文件\n",
    "summary_file = '汇总.xlsx'\n",
    "summary.to_excel(summary_file, index=False)\n",
    "\n",
    "print(\"数据已成功写入汇总文件。\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
